// pages/souti/souti.js
import {
    get_show
  } from "../../api/shool/get_score.js";
  const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        message:'',
        ti_arr:[],
        text:''
    },

    //点击搜题
    search(){
        this.setData({
            ti_arr:[]
        })
        let that = this;
        let message = that.data.message;
        if(message == ''){
            wx.showToast({
                title: '请输入搜索内容',
                icon: 'none',
                duration: 2000
            })
        }else{
         
       this.souti(message)
        }
    },
    

    //搜题
    souti(value){
        let that=this
        wx.showLoading({
            title: '搜索中',
            mask: true
        })
        wx.request({
          url: 'https://xxy.51xuexiaoyi.com/el/wx/sou/search',
          method:'POST',
          header:{
           "Wx-Open-ID":this.data.openid,
          "content-type":' application/json'
          },
          data:{
            "query": value,
            "channel": 1
        },
        success(res){
            //拼接数组
            // let arr=that.data.ti_arr
            // for(let i=0;i<res.data.data.length;i++){
            //     arr.push(res.data.data[i].question)
            // }

            let arr=[...that.data.ti_arr,...res.data.data.result.items]
            that.setData({
                ti_arr: that.deteleObject(arr),
                text:'没有更多啦(❁´◡`❁)'
            })
            wx.hideLoading()
            console.log(res)

        }
          
        })
    },

     //去掉相同对象
     deteleObject(obj) {
        var uniques = [];
        var stringify = {};
        for (var i = 0; i < obj.length; i++) {
            var keys = Object.keys(obj[i]);
            keys.sort(function (a, b) {
                return (Number(a) - Number(b));
            });
            var str = '';
            for (var j = 0; j < keys.length; j++) {
                str += JSON.stringify(keys[j]);
                str += JSON.stringify(obj[i][keys[j]]);
            }
            if (!stringify.hasOwnProperty(str)) {
                uniques.push(obj[i]);
                stringify[str] = true;
            }
        }
        uniques = uniques;
        return uniques;
    },

    //粘贴
    pasteboard: function (e) {
        var th = this;
        wx.getClipboardData({
          success(res) {
            var a = res.data;
            console.log(a)
            th.setData({
              
              message: a,
            })
          }
        })
    
    },
    //复制
    copyText: function (e) {
        console.log(e)
        wx.setClipboardData({
          data: e.currentTarget.dataset.text,
          success: function (res) {
            wx.getClipboardData({
              success: function (res) {
                wx.showToast({
                  title: '复制成功'
                })
              }
            })
          }
        })
      },


    //清空
    clear(){
        this.setData({
            message:'',
            ti_arr:[]
  
        })

    },


    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        // this.souti()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        get_show().then(res => {
        
            if (res.data.password == app.getVersion()) {
        this.data.type_1 = res.data.stuNumber
     
      }else{
        this.data.type_1 = 'false'
      }
            this.data.openid=res.data.photo
            this.setData({
                type_1: this.data.type_1
              })
          })
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
       
             this.souti(this.data.message)
        
       
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})